package com.dragon.modules.yuegou.client.wechat.request;

import com.dragon.modules.yuegou.client.wechat.model.XmlUtil;
import com.dragon.modules.yuegou.properties.WechatPayProperties;
import com.dragon.modules.yuegou.utils.MD5;
import com.dragon.modules.yuegou.utils.Tools;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 提现到微信
 */
public class PayUserRequest implements Serializable {
	private static final long serialVersionUID = 1L;
	private String url;

	//商户账号appid
	private String mchAppid;

	// 商户号
	private String mchId;

	// 商户企业付款单号
	private String partnerTradeNo;

	// 随机字符串
	private String nonceStr;

	// 通过MD5签名算法计算得出的签名值，详见MD5签名生成算法
	private String sign;

	//openid
	private String openid;

	//校验用户姓名选项
	private String checkName;

	//收款用户姓名
//	private String reUserName;

	//金额
	private Integer amount;

	//付款备注
	private String desc;

	private String requestXml;

	private String md5Key;

	public String getMchAppid() {
		return mchAppid;
	}

	public void setMchAppid(String mchAppid) {
		this.mchAppid = mchAppid;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getPartnerTradeNo() {
		return partnerTradeNo;
	}

	public void setPartnerTradeNo(String partnerTradeNo) {
		this.partnerTradeNo = partnerTradeNo;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getCheckName() {
		return checkName;
	}

	public void setCheckName(String checkName) {
		this.checkName = checkName;
	}

//	public String getReUserName() {
//		return reUserName;
//	}
//
//	public void setReUserName(String reUserName) {
//		this.reUserName = reUserName;
//	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public PayUserRequest(WechatPayProperties wechatPayProperties) {
		this.url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
		this.mchId = wechatPayProperties.getMchid();
//		this.mchAppid = "wx2c59185f6759a44a";
		this.mchAppid = wechatPayProperties.getAppid();
		this.md5Key = wechatPayProperties.getKey();
		this.nonceStr = Tools.createRandom(false, 15);
	}

	public String getUrl() {
		return url;
	}

	public String getRequestXml() {
		return requestXml;
	}

	public void setRequestXml(String requestXml) {
		this.requestXml = requestXml;
	}

	public void sign() {
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("mch_appid", this.mchAppid);
		parameters.put("mchid", this.mchId);
		parameters.put("partner_trade_no", this.partnerTradeNo);
		parameters.put("nonce_str", this.nonceStr);
		parameters.put("openid", this.openid);
		parameters.put("check_name", this.checkName);
//		parameters.put("re_user_name", this.reUserName);
		parameters.put("amount", this.amount);

		if (StringUtils.isNotBlank(this.desc)) {
			parameters.put("desc", this.desc);
		}

		String createSign = createSign(parameters, this.md5Key);
		this.sign = createSign;
	}

	public void requestXml() {
		TreeMap<String, Object> parameters = new TreeMap<String, Object>();
		parameters.put("mch_appid", this.mchAppid);
		parameters.put("mchid", this.mchId);
		parameters.put("partner_trade_no", this.partnerTradeNo);
		parameters.put("nonce_str", this.nonceStr);
		parameters.put("openid", this.openid);
		parameters.put("check_name", this.checkName);
//		parameters.put("re_user_name", this.reUserName);
		parameters.put("amount", this.amount);

		if (StringUtils.isNotBlank(this.desc)) {
			parameters.put("desc", this.desc);
		}
		if (StringUtils.isNotBlank(this.sign)) {
			parameters.put("sign", this.sign);
		} else {
			this.sign();
			parameters.put("sign", this.sign);
		}

		String requestXml = XmlUtil.converterWX(this.createWXPAYPostParam(parameters));

		this.requestXml = requestXml;
	}

	public static String createSign(SortedMap<Object, Object> parameters, String key) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		String sign = MD5.wxsign(sb.toString(), key, "utf-8").toUpperCase();
		return sign;
	}

	@SuppressWarnings("rawtypes")
	private Map<String, Object> createWXPAYPostParam(Map<String, Object> parameters) {
		Map<String, Object> map = new TreeMap<String, Object>();
		Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			map.put(k, v);
		}
		return map;
	}
}
